package com.tanhua.dubbo.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mapper.BlackListMapper;
import com.tanhua.dubbo.api.mapper.QuestionMapper;
import com.tanhua.dubbo.api.mapper.SettingsMapper;
import com.tanhua.dubbo.api.mapper.UserInfoMapper;
import com.tanhua.model.db.*;
import com.tanhua.model.vo.*;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private SettingsMapper settingsMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private BlackListMapper blackListMapper;

    /**
     * 新用户注册登录时设置用户信息
     *
     * @param userInfo 前端传回来的用户信息
     * @return 返回用户ID
     */
    @Override
    public Long AddUserInfo(UserInfo userInfo) {
        int insert = userInfoMapper.insert(userInfo);

        return userInfo.getId();
    }

    /**
     * 更新用户信息添加头像图片路径
     *
     * @param userInfo
     */
    @Override
    public void updeteUser(UserInfo userInfo) {
        LambdaQueryWrapper<UserInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(UserInfo::getId, userInfo.getId());
        int update = userInfoMapper.update(userInfo, qw);

    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @param huanxinId
     * @return
     */
    @Override
    public UserInfo getuserinfo(Long userId, Long huanxinId) {
        LambdaQueryWrapper<UserInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(UserInfo::getId, userId);
        return userInfoMapper.selectOne(qw);
    }

    /**
     * 更新用户信息
     *
     * @param userInfoVo
     */
    @Override
    public void updeteByuserId(UserInfoVo userInfoVo) {
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(userInfoVo, userInfo);
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 更新用户头像
     */
    @Override
    public void updateAvatar(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 获取通用设置
     *
     * @param user
     * @return
     */
    @Override
    public SettingsVo getsetting(User user) {
        SettingsVo vo = new SettingsVo();
        LambdaQueryWrapper<Settings> qw = new LambdaQueryWrapper<>();
        qw.eq(Settings::getUserId, user.getId());
        Settings settings = settingsMapper.selectOne(qw);
        if (BeanUtil.isEmpty(settings)) {
            Settings selectOne = new Settings();
            selectOne.setLikeNotification(true);
            selectOne.setPinglunNotification(true);
            selectOne.setGonggaoNotification(true);
            selectOne.setUserId(user.getId());
            settingsMapper.insert(selectOne);
            BeanUtils.copyProperties(selectOne, vo);
        }
        LambdaQueryWrapper<Question> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(Question::getUserId, user.getId());
        Question question = questionMapper.selectOne(qw1);
        if (BeanUtil.isEmpty(question)) {
            Question question1 = new Question();
            question1.setTxt("你喜欢什么颜色");
            question1.setUserId(user.getId());
            questionMapper.insert(question1);
            vo.setStrangerQuestion(question1.getTxt());
            vo.setPhone(user.getMobile());
            vo.setId(question1.getId());
            return vo;
        }
        vo.setStrangerQuestion(question.getTxt());
        vo.setPhone(user.getMobile());
        vo.setId(user.getId());
        return vo;
    }

    /**
     * 查看黑名单
     *
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<UserInfoVoAge> getbalckList(Long userId, Long page, Long pagesize) {
        LambdaQueryWrapper<BlackList> qw = new LambdaQueryWrapper<>();
        qw.eq(BlackList::getUserId, userId);
        Page<BlackList> pages = new Page<>();
        Page<BlackList> blackListPage = blackListMapper.selectPage(pages, qw);
        List<BlackList> records = blackListPage.getRecords();
        List<UserInfoVoAge> vo = records.stream().map(itm -> {
            UserInfoVoAge age = new UserInfoVoAge();
            UserInfo userInfo = userInfoMapper.selectById(itm.getBlackUserId());
            BeanUtils.copyProperties(userInfo, age);
            return age;
        }).collect(Collectors.toList());

        return new PageResult<>(blackListPage.getCurrent(), pagesize, blackListPage.getPages(), page, vo);
    }

    /**
     * 删除黑名单
     *
     * @param uid
     */
    @Override
    public void deleteByid(Long uid) {
        LambdaQueryWrapper<BlackList> qw = new LambdaQueryWrapper<BlackList>();
        qw.eq(BlackList::getBlackUserId, uid);
        blackListMapper.delete(qw);
    }

    /**
     * 获取推荐列表
     */
    @Override
    public Map<Long, UserInfo> getUserList(List<Long> userIds, UserInfo condition) {
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        Map<Long, UserInfo> userInfoMap = new HashMap<>();
        // 1. 条件不为空
        if (null != condition) {
            // 2. 判断有年龄, 添加年龄，实现<=查询
            if (null != condition.getAge()) {
                lqw.le(UserInfo::getAge, condition.getAge());
            }
            // 3. 有性别，则添加为条件
            if (StringUtils.isNotEmpty(condition.getGender())) {
                lqw.eq(UserInfo::getGender, condition.getGender());
            }
            lqw.in(UserInfo::getId, userIds);
            // 4. 查询，得到的是list
            List<UserInfo> userInfoList = userInfoMapper.selectList(lqw);
            // 非空
            if (CollectionUtil.isNotEmpty(userInfoList)) {
                // key=用户的id, value=u代表集合中的每个元素 userInfo
                //userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, u -> u));
                for (UserInfo userInfo : userInfoList) {
                    userInfoMap.put(userInfo.getId(), userInfo);
                }
            }
        }
        // 5. list转成map
        return userInfoMap;
    }

    /**
     * 探花
     *
     * @return
     */
    @Override
    public List<UserInfo> getAllUser(Long userId) {
        LambdaQueryWrapper<UserInfo> qw = new LambdaQueryWrapper<>();
        qw.ge(UserInfo::getId, 1);
        qw.notIn(UserInfo::getId, 1);
        List<UserInfo> userInfoList = userInfoMapper.selectList(qw);

        return userInfoList;
    }

    /**
     * 批量获取用户信息
     *
     * @param ids
     * @return
     */
    @Override
    public List<UserInfo> getUsersByid(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return null;
        }
        if (CollectionUtil.isNotEmpty(ids) && ids.size() == 1) {
            List<UserInfo> list = new ArrayList<>();
            for (Long id : ids) {
                UserInfo userInfo = userInfoMapper.selectById(id);
                list.add(userInfo);
            }
            return list;
        }
        List<UserInfo> userInfoList = userInfoMapper.selectBatchIds(ids);
        return userInfoList;
    }
    /**
     *  用户数据翻页
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public Page<UserInfo> getUsers(Long page, Long pagesize) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        Page<UserInfo> infoPage = new Page<>(page,pagesize);
        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(infoPage, queryWrapper);
        return userInfoPage;
    }


}
